<?php
  /**
  * Login google with OpenID action
  * @author:trungdt
  * @since: Jan 21, 2011
  * @version: 1
  * 
  * Change logs
  * Jan 29, 2011 - trungdt - Add support login OpenID and automatic login to system
  */
  // include OpenID Class
  if (!defined('EXPONENT')) exit('');  
  include( BASE."modules/openidmodule/openid.php");
  // check for ajax_action param
  if (!isset($_REQUEST['ajax_action']))
  {
      $_REQUEST['ajax_action']=1;
      $_SERVER['REQUEST_URI'].="&ajax_action=1";
  }
  //
  // redirect if logged in
  if (exponent_users_isLoggedIn())
  {
      exponent_flow_redirect();
  }
  else
  {
      try{
          $openid = @new LightOpenID();
          if(!$openid->mode) {
              $openid->identity = 'https://www.google.com/accounts/o8/id';
              //echo $openid->authUrl();
              $openid->required = array(
              'namePerson/friendly',
              'contact/email'       ,
              'namePerson'           ,
              'birthDate'             ,
              'person/gender'          ,
              'contact/postalCode/home' ,
              'contact/country/home'    ,
              'pref/language'          ,
              'pref/timezone'          ,
              'namePerson/first',
              'namePerson/last'
              );
              $openid->optional = array('namePerson/first');
              header('Location: ' . $openid->authUrl());
          }
          else if (($openid->mode == "cancel") || (!$openid->validate()))
          {
              echo "
              <html>
              <body>
              <script language='javascript'>
                 window.opener.login_error(0);
                 window.close();
              </script>
              </body></html>";
          }
          else
          {
                 $temp=$openid->getAttributes();
                 /*
                    Array
                    (
                    [namePerson/first] => Trung
                    [contact/email] => trungdt@absoft.vn
                    [pref/language] => vi
                    [namePerson/last] => Đặng
                    )

                 */
                 $first_name=""; if (isset ($temp['namePerson/first'])) $first_name=$temp['namePerson/first'];
                 $last_name=""; if (isset ($temp['namePerson/last'])) $last_name=$temp['namePerson/last'];
                 $email=""; if (isset ($temp['contact/email'])) $email=$temp['contact/email'];
                 $identity=$openid->identity;
                 //
                 $myobj=null;
                 $myobj->first_name=$first_name;
                 $myobj->last_name=$last_name;
                 $myobj->email=$email;
                 $myobj->indentifer=$identity; 
                 
                 aboid_updateIdentify($myobj);
                 // save to session
                 $temp=$db->selectObject("user","email = '{$email}'");
                 if ($temp)
                 {
                     // okies, this user is exist, we login this user
                     $user=$temp;
                     //Update the last login timestamp for this user.
                     $user->last_login = time();
                     $db->updateObject($user, 'user');

                     // Retrieve the full profile, complete with all Extension data.
                     $user = exponent_users_getFullProfile($user);

                     // Call on the Sessions subsystem to log the user into the site.
                     exponent_sessions_login($user);
                 }
                 // now, we redirect parent page
                 echo "<html>
              <body>
              <script language='javascript'>
                 window.opener.document.location='/';
                 window.close();
              </script>
              </body></html>";
          } 
      } catch (ErrorException $e) {
          //echo $e->getMessage();
      }
  }
  //
?>
